home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Essential Home & Business Collection
/
The Essential Home & Business Collection.iso
/
27
/
3
/
5
/
HP22D5.ZIP
/
EXTERN
/
FORMAT.C
< prev
next >
Wrap
Text File
|
1991-04-16
|
2KB
|
110 lines
#include "extern.h" /* Extensions need these! */
HANDLE pascal near reformat(ptr,width,height)
PTR ptr;
int width,height;
{
PTR temp;
int len;
int slen;
int y = 0;
int i;
int nc;
HANDLE hdl;
int hlen;
hdl = NewHandle(hlen = 1);
**hdl = '\0';
slen = strlen(ptr);
while (slen > 0) {
#pragma loop_opt(off)
for (temp = ptr,len = 0;
*temp && (*temp != '\r') && (len <= width);
temp++,len++);
if (len <= width) len++;
else {
for ( temp = ptr + width,len = width;
(len >= 0) && (*temp != ' ');
len--,temp-- );
if (len < 0) len = width;
else while ((*temp == ' ') && *temp) temp++,len++;
}
#pragma loop_opt(on)
slen -= len;
nc = len - 1;
while ((nc >= 0) && ((ptr[nc] == ' ') || (ptr[nc] == '\r'))) nc--;
nc++;
ReAllocHandle(hdl,hlen + nc + 1); // 1 for CR
strncpy(deref(hdl) + hlen - 1,ptr,nc);
deref(hdl)[hlen + nc - 1] = '\r';
deref(hdl)[hlen + nc] = '\0';
hlen += nc + 1;
y++;
if (y >= height) break;
ptr += len;
}
if (hlen > 1) deref(hdl)[hlen - 2] = '\0';
return(hdl);
}
/*
** This routine formats a block of text within a given width and height.
** It does this by inserting carriage returns into the text at positions
** where a line normally word wraps.
**
** Parameters:
**
** text this is the text that you want formatted
** width this is the width in which lines are broken
** height this is the height in which to format text. If the
** height is too large, all of the text will be formatted.
**
** To call this routine from HyperPAD:
**
** put formatText(page field 1,10,9999) into page field 1;
*/
format(int NumArgs,HANDLE hdl,HANDLE hWidth,HANDLE hHeight)
{
HANDLE newhdl;
int width,height;
PTR p;
if (NumArgs != 3) return(ERROR);
width = htoi(hWidth);
height = htoi(hHeight);
p = LockHandle(hdl);
newhdl = reformat(p,width,height);
ReturnValue(newhdl);
UnLockHandle(hdl);
return(STOP);
}
POOL pascal Pool[] = {
{ "formatText",
format,
0,
FUNCTION},
{ NULL,
NULL,
0,
0} };